package com.antd.admin.server.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.antd.admin.api.entity.SysDictItem;
import com.antd.admin.server.service.ISysDictItemService;
import com.antd.common.core.util.R;
import com.antd.common.core.vo.PageVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典详情表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-06-09
 */
@RestController
@RequestMapping("/item")
@Api("字典值API")
public class SysDictItemController {
    @Autowired
    private ISysDictItemService dictItemService;

    @GetMapping
    @ApiOperation("分页字典值列表")
    @PreAuthorize("hasAnyAuthority('sys:item:list')")
    public R<Object> list(PageVo pageVo, SysDictItem dictItem){
        return R.ok(dictItemService.page(new Page<>(pageVo.getPageNum(),pageVo.getPageSize()), Wrappers
                .<SysDictItem>query()
                .select()
                .lambda()
                .eq(Objects.nonNull(dictItem.getDictId()),SysDictItem::getDictId,dictItem.getDictId())
                .like(StrUtil.isNotBlank(dictItem.getItemText()),SysDictItem::getItemText,dictItem.getItemText())
                .eq(ObjectUtil.isNotNull(dictItem.getStatus()),SysDictItem::getStatus,dictItem.getStatus())));
    }
    @PostMapping
    @ApiOperation("保存字典值")
    @PreAuthorize("hasAnyAuthority('sys:item:save')")
    public R<Object> saveDictItem(@RequestBody SysDictItem dictItem){
        return R.ok(dictItemService.save(dictItem));
    }
    @PutMapping
    @ApiOperation("修改字典值")
    @PreAuthorize("hasAnyAuthority('sys:item:edit')")
    public R<Object> updateDictItem(@RequestBody SysDictItem dictItem){
        return R.ok(dictItemService.updateById(dictItem));
    }
    @DeleteMapping
    @ApiOperation("删除字典值")
    @PreAuthorize("hasAnyAuthority('sys:item:remove')")
    public R<Object> deleteDictItem(String ids){
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        return R.ok(dictItemService.removeByIds(idList));
    }
}
